//
//  UIButton+Extension.swift
//  glzbowei
//
//  Created by 高伦展 on 2017/11/19.
//  Copyright © 2017年 glz. All rights reserved.
//

import UIKit

extension UIButton{
    
    //'便利'构造函数  执行完，对象已经创建好
    convenience init(imgName:String,bgImgName:String?) {
        self.init()  //创建对象
        
        setImage(UIImage(named:imgName), for: UIControlState.normal)
        setImage(UIImage(named:imgName+"_highlighted"), for: UIControlState.highlighted)
        
        if let bgImgName = bgImgName{
            setBackgroundImage(UIImage(named:bgImgName), for: UIControlState.normal)
            setBackgroundImage(UIImage(named:bgImgName+"_highlighted"), for: UIControlState.highlighted)
       }
        //会根据背景图片的大小调整尺寸
        sizeToFit()
        
    }
    
    //'便利'构造函数 title ,color ,imgName
    convenience init(title:String,color:UIColor,backImgName:String){
        self.init()
     
        setTitle(title, for: .normal)
        setTitleColor(color, for: .normal)
        setBackgroundImage(UIImage(named:backImgName), for: .normal)
        
        //会根据背景图片的大小调整尺寸
        sizeToFit()
    }
    
    //'便利'构造函数 title ,color ,imgName
    convenience init(title:String,fontSize:CGFloat,color:UIColor,imgName:String?,backColor:UIColor? = nil){
        self.init()
        
        setTitle(title, for: .normal)
        setTitleColor(color, for: .normal)
        
        if let imgName = imgName {
            setImage(UIImage(named:imgName), for: .normal)
        }
        backgroundColor = backColor
        
        titleLabel?.font = UIFont.boldSystemFont(ofSize: fontSize)
        
        //会根据背景图片的大小调整尺寸
        sizeToFit()
    }
}
